﻿using AutoMapper;
using ManagementApi.Models.Project;
using Minio.DataModel;
using Modules;
using Modules.Attachment;
using Modules.Corp;
using Modules.Person;
using Modules.Project;
using PmSoft;
using System.ComponentModel.DataAnnotations;
using static Modules.Person.PersonConstants;

namespace ManagementApi.Models.Person
{
    public class PersonContractDto
    {
        /// <summary>
        /// 主键
        ///</summary>
        public int Id { get; set; }

        /// <summary>
        /// 合同编号
        ///</summary>
        public string Code { get; set; }
        /// <summary>
        /// 项目Id
        ///</summary>
        public int ProjectId { get; set; }
        /// <summary>
        /// 人员Id
        ///</summary>
        public int PersonId { get; set; }

        /// <summary>
        /// 人员姓名
        /// </summary>
        public string PersonName { get; set; }

        /// <summary>
        /// 身份证号
        /// </summary>
        public string CardNumber { get; set; }

        /// <summary>
        /// 企业Id
        ///</summary>
        public int CorpId { get; set; }

        /// <summary>
        /// 企业名称
        ///</summary>
        public string CorpName { get; set; }

        /// <summary>
        /// 合同类型
        ///</summary>
        public 合同类型 Type { get; set; }

        public 合同签订类型 SigningType { get; set; }
        public string SigningTypeName { get; set; }

        /// <summary>
        /// 合同类型
        /// </summary>
        public string TypeName { get; set; }

        /// <summary>
        /// 合同签订日期
        ///</summary>
        public DateTime SigningDate { get; set; }

        /// <summary>
        /// 合同起始日期
        ///</summary>
        public DateTime StartDate { get; set; }

        /// <summary>
        /// 合同终止日期
        ///</summary>
        public DateTime EndDate { get; set; }

        /// <summary>
        /// 工资计算方式
        ///</summary>
        public 工资计算方式 SalaryCalculationMethod { get; set; }

        /// <summary>
        /// 工资计算方式
        /// </summary>
        public string SalaryCalculationMethodName { get; set; }

        /// <summary>
        /// 工资标准
        ///</summary>
        public decimal WageStandard { get; set; }

        /// <summary>
        /// 工资支付日期
        ///</summary>
        public int SalaryPaymentDate { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateDate { get; set; }

        /// <summary>
        /// 合同附件
        /// </summary>
        public IEnumerable<AttachmentsForm<int>> Attachments { get; set; }
    }

    public static partial class PersonContractExceptions
    {
        public static IEnumerable<PersonContractDto> AsDtos(this IEnumerable<PersonCorpContract> data)
        {
            List<PersonContractDto> dtos = new();
            foreach (var item in data)
            {
                dtos.Add(item.AsDto());
            }
            return dtos;
        }

        public static PersonContractDto AsDto(this PersonCorpContract entity)
        {
            PersonContractDto dto = new();
            dto = ServiceAccessor.GetService<IMapper>().Map(entity, dto);
            var corpService = ServiceAccessor.GetService<CorpService>();
            CorpInfo? corpInfo = corpService.GetCorpInfo(entity.CorpId);
            if (corpInfo != null)
                dto.CorpName = corpInfo.Name;

            var PersonService = ServiceAccessor.GetService<PersonService>();
            PersonInfo? person = PersonService.GetPerson(entity.PersonId);

            if (person != null)
            {
                dto.PersonName = person.Name;
                dto.CardNumber = person.CardNumber;
            }
            dto.TypeName = dto.Type.GetDescription();
            dto.SigningTypeName = dto.SigningType.GetDescription();
            dto.SalaryCalculationMethodName = dto.SalaryCalculationMethod.GetDescription();

            return dto;
        }
    }
}